package zuoye;
//业务层（service）：实现鲜花的管理，比如初始化订单、查询、修改、删除。
//在项目中，就是 FlowerMgr 类。
import java.util.Scanner;

public class FlowerMgr {
    Flower[] flowers = new Flower[5]; // 存放订单的数组
    Scanner sc = new Scanner(System.in);

    // 初始化订单
    public void initial() {
        flowers[0] = new Flower(1, "玫瑰", 10, 5);
        flowers[1] = new Flower(2, "百合", 15, 3);
        flowers[2] = new Flower(3, "康乃馨", 8, 6);
    }

    // 主菜单
    public void mainMenu() {
        while (true) {
            System.out.println("\n===== 七彩鲜花销售系统 =====");
            System.out.println("1. 查询订单");
            System.out.println("2. 修改订单");
            System.out.println("3. 删除订单");
            System.out.println("0. 退出系统");
            System.out.print("请选择: ");
            int choice = sc.nextInt();

            switch (choice) {
                case 1: searchAll(); break;
                case 2: updateSale(); break;
                case 3: deleteSale(); break;
                case 0: System.out.println("退出系统。"); return;
                default: System.out.println("无效输入！");
            }
        }
    }

    // 查询订单
    private void searchAll() {
        System.out.println("----- 当前订单信息 -----");
        for (Flower f : flowers) {
            if (f != null) {
                System.out.println(f);
            }
        }
    }

    // 修改订单
    private void updateSale() {
        System.out.print("请输入要修改的订单编号: ");
        int id = sc.nextInt();

        Flower f = findById(id);
        if (f == null) {
            System.out.println("该订单不存在！");
            return;
        }

        System.out.println("找到订单: " + f);
        System.out.print("请输入新的数量: ");
        int newCount = sc.nextInt();

        System.out.print("是否保存修改？(y/n): ");
        String confirm = sc.next();

        if (confirm.equalsIgnoreCase("y")) {
            f.count = newCount;
            System.out.println("修改成功！新订单信息: " + f);
        } else {
            System.out.println("取消修改。");
        }
    }

    // 删除订单
    private void deleteSale() {
        System.out.print("请输入要删除的订单编号: ");
        int id = sc.nextInt();

        for (int i = 0; i < flowers.length; i++) {
            if (flowers[i] != null && flowers[i].id == id) {
                System.out.println("找到订单: " + flowers[i]);
                System.out.print("确认删除吗？(y/n): ");
                String confirm = sc.next();
                if (confirm.equalsIgnoreCase("y")) {
                    flowers[i] = null;
                    System.out.println("删除成功！");
                    searchAll();
                } else {
                    System.out.println("取消删除。");
                }
                return;
            }
        }
        System.out.println("该订单不存在！");
    }

    // 辅助方法：按编号查找
    private Flower findById(int id) {
        for (Flower f : flowers) {
            if (f != null && f.id == id) {
                return f;
            }
        }
        return null;
    }
}
